There are various ways to create interactive visualizations. We present two very popular packages, that adapt ggplots syntax, making them very comfortable to use.
Ther are two ways to make use of plotlys framework.
ggplotly & plotly for R ggplotly turns your ggplot graphs with the function ggplotly() into an interactive graph.
plotly uses a different syntax style, but provides more interactive capabilities
library(plotly)
library(broom)
library(dplyr)
data(diamonds, package = "ggplot2")
data <- sample_n(diamonds, 300)
modlss <- loess(price ~ carat, data = data)
fit <- arrange(augment(modlss,se_fit = TRUE), carat)
p <- ggplot(NULL, mapping = aes(x = carat, y = price )) +
geom_ribbon(data = fit, mapping = aes(ymin = .fitted - 2*.se.fit, ymax = .fitted + 2*.se.fit), fill = "grey70")+
geom_point(data = data, alpha = 0.7, mapping = aes(size = depth, group = cut, color = cut)) +
geom_line(data = fit, aes(x = carat, y = .fitted))
ggplotly(p)
The main difference in ggplot2’s geom_ functions and hc_add_series is that we need to add data and aesthetics explicitly in every function while in ggplot2 one can add data and aesthetics in a layer and then can further add more geoms which can work on same data and aesthetics.
An accurate example is given below using the diamond dataset in the ggplot2 package.
library(highcharter)
library(broom)
library(dplyr)
data(diamonds, package = "ggplot2")
data <- sample_n(diamonds, 300)
modlss <- loess(price ~ carat, data = data)
fit <- arrange(augment(modlss,se_fit = TRUE), carat)
highchart() %>%
hc_add_series(data, type = "scatter",
hcaes(x = carat, y = price, size = depth, group = cut)) %>%
hc_add_series(fit, type = "line", hcaes(x = carat, y = .fitted),
name = "Fit", id = "fit") %>%
hc_add_series(fit, type = "arearange",
hcaes(x = carat, low = .fitted - 2*.se.fit,
high = .fitted + 2*.se.fit),
linkedTo = "fit")
The main difference in ggplot2’s geom_ functions and hc_add_series is that we need to add data and aesthetics explicitly in every function while in ggplot2 one can add data and aesthetics in a layer and then can further add more geoms which can work on same data and aesthetics.
An accurate example is given below using the diamond dataset in the ggplot2 package.
library(plotly)
library(broom)
library(dplyr)
data(diamonds, package = "ggplot2")
data <- sample_n(diamonds, 300)
modlss <- loess(price ~ carat, data = data)
fit <- augment(modlss, se_fit = TRUE)
fig <- plot_ly(data = data, x = ~carat) %>%
add_markers(y = ~price,
color = ~cut,
size = ~carat,
text = ~paste("Price: ", price, '$<br>Cut:', cut)) %>%
add_lines(y = ~fitted(loess(price ~ carat)),
line = list(color = 'lightblue'),
name = "Loess Smoother") %>%
add_ribbons(data = augment(loess(price ~ carat,data = data),se_fit = TRUE),
ymin = ~.fitted - 2 * .se.fit,
ymax = ~.fitted + 2 * .se.fit,
line = list(color = "rgba(2, 162, 182, 0.05)"),
fillcolor = "rgba(2, 162, 182, 0.2)",
name = "Standarderror"
)
fig
# highchart() %>%
# hc_add_series(data, type = "scatter",
# hcaes(x = carat, y = price, size = depth, group = cut)) %>%
# hc_add_series(fit, type = "line", hcaes(x = carat, y = .fitted),
# name = "Fit", id = "fit") %>%
# hc_add_series(fit, type = "arearange",
# hcaes(x = carat, low = .fitted - 2*.se.fit,
# high = .fitted + 2*.se.fit),
# linkedTo = "fit")
Exercise 1.
library(DT)
## Warning: package 'DT' was built under R version 4.1.2
data(diamonds, package = "ggplot2") # load data
data <- diamonds[sample(nrow(diamonds), 300),1:5] #create a subset of the dataset
datatable(data, filter = 'top',
options = list( searchHighlight = TRUE, pageLength = 15 ))
Using ioslides we can also implement interactive plots in our presentations!
---
title: Do Natural Disasters Stimulate Environmental Attitudes? Evidence from a Natural
Experiment.
author: "Hamid Bulut"
date: "27/4/2022"
output:
ioslides_presentation:
css: www/styles.css
widescreen: yes
smaller: yes
highlight: monochrome
slidy_presentation:
highlight: monochrome
beamer_presentation:
highlight: monochrome
subtitle: A Regression Discontinuity Design Analysis
editor_options:
markdown:
wrap: 72
---
knitr::include_url("index_files/slides/slides1.html", height = "800px")